package com.soap.desensitize.factory.encry.rreversible.symmetry;

import com.soap.desensitize.encry.rreversible.symmetry.AbstractSymmetryEncry;
import com.soap.desensitize.exception.factory.FactoryException;
import com.soap.desensitize.exception.factory.StrategyFactoryException;
import com.soap.desensitize.factory.encry.EStrategy;
import com.soap.desensitize.strategy.rreversible.symmetry.AesStrategy;

/**
 * @desc 可逆不对称加密策略的工厂
 */
public class SymmetryFactory {
    /**
     * @desc 根据Class 生产策略
     * @param cla
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public static AbstractSymmetryEncry create(Class<? extends AbstractSymmetryEncry> cla) throws InstantiationException, IllegalAccessException {
        return cla.newInstance();
    }

    /**
     * @desc 根据类路径生产策略
     * @param cla 策略类Class
     * @param password  加密密码
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws StrategyFactoryException
     */
    public static AbstractSymmetryEncry create(Class<? extends AbstractSymmetryEncry> cla,String password) throws InstantiationException, IllegalAccessException, StrategyFactoryException {
        AbstractSymmetryEncry strategy = cla.newInstance();
        strategy.setPassword(password);
        return strategy;
    }

    /**
     * @desc 根据类路径生产策略
     * @param classStr 策略类路径
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws StrategyFactoryException
     */
    public static AbstractSymmetryEncry create(String classStr) throws InstantiationException, IllegalAccessException, StrategyFactoryException {
        switch (classStr){
            case "com.rj.bd.strategy.rreversible.symmetry.AesStrategy":
                return AesStrategy.getInstance();
            case "com.rj.bd.strategy.rreversible.symmetry.DesStrategy":
                return AesStrategy.getInstance();
            case "com.rj.bd.strategy.rreversible.symmetry.NumStrategy":
                return AesStrategy.getInstance();
            case "com.rj.bd.strategy.rreversible.symmetry.Rc4Strategy":
                return AesStrategy.getInstance();
            case "com.rj.bd.strategy.rreversible.symmetry.ThreeDesStrategy":
                return AesStrategy.getInstance();
        }
        throw new StrategyFactoryException("没有找到这个策略 "+classStr);
    }

    /**
     * @desc 根据类路径生产策略
     * @param eStrategy 策略类枚举
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws StrategyFactoryException
     */
    public static AbstractSymmetryEncry create(EStrategy eStrategy) throws FactoryException {
        try {
            return create(eStrategy.getClassName());
        } catch (InstantiationException e) {
            throw new FactoryException("实例化异常 InstantiationException");
        } catch (IllegalAccessException e) {
            throw new FactoryException("非法访问异常 IllegalAccessException");
        } catch (StrategyFactoryException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @desc 根据类路径生产策略
     * @param eStrategy 策略类枚举
     * @param password  加密密码
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws StrategyFactoryException
     */
    public static AbstractSymmetryEncry create(EStrategy eStrategy,String password) throws InstantiationException, IllegalAccessException, StrategyFactoryException {
        AbstractSymmetryEncry strategy = create(eStrategy.getClassName());
        strategy.setPassword(password);
        return strategy;
    }
}
